package com.neuedu.controller.backend;

import com.neuedu.common.Const;
import com.neuedu.common.JSONData;
import com.neuedu.pojo.Product;
import com.neuedu.pojo.Specs;
import com.neuedu.pojo.UserInfo;
import com.neuedu.service.SpecsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Api(value = "/mananger/specs",description = "后台规格相关接口")
@Controller
@RequestMapping("/manager/specs")
public class SpecsController {
    @Autowired
    private SpecsService specsService;
    @ApiOperation(value = "规格信息的保存与修改",notes = "规格信息的保存与修改",httpMethod = "POST")
    @RequestMapping(value = "saveOrUpdateSpecs",method = RequestMethod.POST)
    @ResponseBody
    public JSONData<String> saveOrUpdateSpecs(HttpSession session, Specs specs){
        // 1、判断用户是否登录
        UserInfo userInfo = (UserInfo) session.getAttribute(Const.CURRENT_USERINFO);
        if(userInfo == null){
            return JSONData.buildError("用户未登录，请先登录");
        }
        //2、 判断其是否是管理员
        if(userInfo.getRole()==0){
            JSONData<String> data = specsService.saveOrUpdateSpecs(specs);
            return data;
        }else{
            return JSONData.buildError("此用户无权限，请联系管理员！");
        }
    }
    @ApiOperation(value = "查询所有规格信息",notes = "查询所有规格信息",httpMethod = "POST")
    @RequestMapping(value = "queryAllSpecs",method = RequestMethod.POST)
    @ResponseBody
    public JSONData<List<Specs>> queryAllSpecs(HttpSession session){
        // 1、判断用户是否登录
        UserInfo userInfo = (UserInfo) session.getAttribute(Const.CURRENT_USERINFO);
        if(userInfo == null){
            return JSONData.buildError("用户未登录，请先登录");
        }
        //2、 判断其是否是管理员
        if(userInfo.getRole()==0){
            JSONData<List<Specs>> data = specsService.queryAllSpecs();
            return data;
        }else{
            return JSONData.buildError("此用户无权限，请联系管理员！");
        }
    }
    @ApiOperation(value = "根据ID查询规格信息",notes = "根据ID查询规格信息",httpMethod = "POST")
    @RequestMapping(value = "querySpecsById",method = RequestMethod.POST)
    @ResponseBody
    public JSONData<Specs> querySpecsById(HttpSession session,Integer id){
        // 1、判断用户是否登录
        UserInfo userInfo = (UserInfo) session.getAttribute(Const.CURRENT_USERINFO);
        if(userInfo == null){
            return JSONData.buildError("用户未登录，请先登录");
        }
        //2、 判断其是否是管理员
        if(userInfo.getRole()==0){
            JSONData<Specs> data = specsService.querySpecsById(id);
            return data;
        }else{
            return JSONData.buildError("此用户无权限，请联系管理员！");
        }
    }
    @ApiOperation(value = "根据ID删除规格信息",notes = "根据ID删除规格信息",httpMethod = "POST")
    @RequestMapping(value = "delSpecsById",method = RequestMethod.POST)
    @ResponseBody
    public JSONData<String> delSpecsById(HttpSession session,Integer id){
        // 1、判断用户是否登录
        UserInfo userInfo = (UserInfo) session.getAttribute(Const.CURRENT_USERINFO);
        if(userInfo == null){
            return JSONData.buildError("用户未登录，请先登录");
        }
        //2、 判断其是否是管理员
        if(userInfo.getRole()==0){
            JSONData<String> data = specsService.delSpecsById(id);
            return data;
        }else{
            return JSONData.buildError("此用户无权限，请联系管理员！");
        }
    }
}
